/**
 * created by haitao.yao @ Feb 22, 2011
 */
package com.googlecode.storage;

import java.nio.ByteBuffer;

/**
 * 
 * @author haitao-yao
 * 
 */
public class MemorySlab {

	private final long capacity;

	private final ByteBuffer nativeByteBuffer;

	/**
	 * 
	 * @param capacity
	 *            size of memory pool, unit: byte
	 */
	public MemorySlab(int capacity) {
		super();
		if (capacity < 1024) {
			throw new IllegalArgumentException(
					"index memory pool should be greater than 1K(1024byte)");
		}
		this.capacity = capacity;
		try {
			nativeByteBuffer = ByteBuffer.allocateDirect(capacity);
		} catch (Throwable e) {
			throw new IllegalArgumentException(
					"can't allocate native byte buffer, capacity: "
							+ capacity
							+ ", check the argument or jvm XX:MaxDirectMemorySize settings");
		}
	}

	public long capacity() {
		return capacity;
	}

	public ValuePhysicalPosition getValuePhisicalPosition(int start) {
		return ValuePhysicalPosition.read(start, nativeByteBuffer);
	}
	
	
}
